#include <QSplashScreen>
#include <QMainWindow>
#include <QSettings>
#include <QProgressDialog>
#include <QTranslator>
#include <QApplication>

#include "mvc/view/view.h"
#include "mvc/model/model.h"
#include "mvc/controller/controller.h"
#include "mvc/view/geometries_widget.h"
#include "common/log/QsLog.h"
#include "common/settings.h"
#include "common/modules.h"

#include <iostream>


void initialize_settings();
QMainWindow *init_mvc();

View *view;
Model *model;
Controller *controller;
static QApplication *app;
QMainWindow *mainWin;

int main(int argc, char *argv[])
{
    app = new QApplication(argc, argv);

    QsLogging::Logger& logger = QsLogging::Logger::instance();
     logger.setLoggingLevel(QsLogging::TraceLevel);

     QString pathLog;
     pathLog.append(log_directory()).append("uav.log");

    const QString sLogPath(pathLog);
    QsLogging::DestinationPtr fileDestination(
        QsLogging::DestinationFactory::MakeFileDestination(sLogPath, false) );
    QsLogging::DestinationPtr debugDestination(
        QsLogging::DestinationFactory::MakeDebugOutputDestination() );
    logger.addDestination(debugDestination);
    logger.addDestination(fileDestination);


    QLOG_INFO() << "*******************************************";
    QLOG_INFO() << "Program started";
    QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();


    //QLOG_ERROR() << "An error has occurred";

    QSplashScreen *splash = new QSplashScreen;

    splash->setPixmap(QPixmap(":/images/GeParLogo.png"));
    splash->show();

    Qt::Alignment bottomRight = Qt::AlignRight | Qt::AlignBottom;

    splash->showMessage(QObject::tr("Setting up the main window..."), bottomRight, Qt::black);

    mainWin = init_mvc();

    splash->showMessage(QObject::tr("Loading settings..."), bottomRight, Qt::black);

    initialize_settings();

    mainWin->showMaximized();

    //mainWin->setCentralWidget(new Geometries_widget());
    splash->finish(mainWin);

    delete splash;

    return app->exec();
}


/**
  * Instalation configuration
  */


QMainWindow *init_mvc()
{
    view = new View();
    model = new Model();
    controller = new Controller();

    model->subscribe(view);
    view->set_model(model);
    controller->set_view(view);
    controller->set_model(model);
    controller->connect_signals();

    return (QMainWindow *) (view->get_principal_window());
}

void initialize_settings()
{
    add_module(uavs_jw_module);
    add_module(conventional_w_module);
    add_module(bwb_module);
}
